home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
utility.lha
/
utility
/
strings.H
< prev
Wrap
C/C++ Source or Header
|
1993-08-08
|
4KB
|
137 lines
// This is a simple string package for C++. Strings have automatic
// storage management and value semantics. Copying of the string
// contents is common in this implementation.
//
// The string object has a small internal buffer that is used for short
// strings. This usually saves a great number memory allocations (in
// one test 80% of all strings were stored in the local buffer).
//
// This header file also declares a list of strings, LIST(String),
// a string iterator, ITERATOR(String), and output on i/o streams.
//
// .SS History
// Author: Dag Bruck.
//
// $Id: strings.H,v 1.3 91/08/28 11:33:40 dag Exp $
#ifndef STRINGS_H
#define STRINGS_H
#include <defs.H>
#include <list.H>
#include <listiter.H>
#include <iostream.h>
#include <string.h>
class String {
public:
String();
// Constructs a string of zero length.
String(const char *);
// Constructs a string from a character array.
String(const String &);
// Constructs a copy of another string.
~String();
// Destroys string, deallocating storage.
String& operator = (const String &);
String& operator = (const char *);
String& operator = (char);
// Assignment to string.
void operator += (const String &);
void operator += (char);
// Appends a string or character to a string.
char operator () (unsigned i) const;
// Returns i'th character of string. Strings start at 0 (zero).
// Returns zero if index if out of bounds.
String operator () (unsigned start, unsigned n) const;
// Returns the substring starting at `start' which is at
// most `n' characters long. Strings start at 0 (zero).
operator const char * () const;
// Returns pointer to contents of string.
char* Copy() const;
// Returns a copy of the string contents. The copy is
// allocated with new and must be deleted by the caller.
String upper() const;
String lower() const;
// Returns a string where every lower (upper) case letter is converted
// to upper (lower) case, according to ctype(3). Other characters are
// copied unchanged.
unsigned Length() const;
// Returns length of string (excluding terminating null).
unsigned Size() const;
// Returns number of bytes allocated for string contents.
unsigned Hashval() const;
// Returns an unsigned integer suitable as index in hashtables, etc.
private:
char* str;
// Pointer to allocated storage.
unsigned size;
// Size of allocated storage for string (bytes).
enum {internal_buffer_size = 8};
char buf[internal_buffer_size];
// Internal buffer for short strings; saves free store allocations.
friend ostream& operator << (ostream &, const String &);
// Inserts a string in the output stream. The same formatting
// as for char* takes place.
friend istream& operator >> (istream &, String &);
// Extracts a string from an input stream. Initial whitespace
// is skipped. The string extends until the next whitespace.
friend boolean operator == (const String &, const String &);
friend boolean operator == (const String &, const char *);
friend boolean operator == (char *, const String &);
// Tests for equality ("not equal" also defined).
};
String operator + (const String &, const String &);
// Concatenates two strings.
inline unsigned String :: Size() const
{ return size; }
inline unsigned String :: Length() const
{ return strlen(str); }
inline String :: operator const char * () const
{ return str; }
inline boolean operator == (const String& s1, const String& s2)
{ return s1.str[0] == s2.str[0] && strcmp(s1.str, s2.str) == 0; }
inline boolean operator != (const String& s1, const String& s2)
{ return !(s1 == s2); }
inline boolean operator != (const String& s1, const char* s2)
{ return !(s1 == s2); }
inline boolean operator != (char* s1, const String& s2)
{ return !(s1 == s2); }
LIST_DECLARE(String);
ITERATOR_DECLARE(String);
#endif